libm5: Create a libm5 static library for embedding m5.
[gem5.git] / src / sim / init.cc
1 /*
2 * Copyright (c) 2000-2005 The Regents of The University of Michigan
3 * Copyright (c) 2008 The Hewlett-Packard Development Company
4 * All rights reserved.
5 *
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.
16 *
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.
28 *
29 * Authors: Nathan Binkert
30 */
31
32 #include <Python.h>
33 #include <marshal.h>
34 #include <signal.h>
35
36 #include <iostream>
37 #include <string>
38 #include <zlib.h>
39
40 #include "base/cprintf.hh"
41 #include "base/misc.hh"
42 #include "sim/async.hh"
43 #include "sim/core.hh"
44 #include "sim/host.hh"
45 #include "sim/init.hh"
46
47 using namespace std;
48
49 /// Stats signal handler.
50 void
51 dumpStatsHandler(int sigtype)
52 {
53 async_event = true;
54 async_statdump = true;
55 }
56
57 void
58 dumprstStatsHandler(int sigtype)
59 {
60 async_event = true;
61 async_statdump = true;
62 async_statreset = true;
63 }
64
65 /// Exit signal handler.
66 void
67 exitNowHandler(int sigtype)
68 {
69 async_event = true;
70 async_exit = true;
71 }
72
73 /// Abort signal handler.
74 void
75 abortHandler(int sigtype)
76 {
77 ccprintf(cerr, "Program aborted at cycle %d\n", curTick);
78 }
79
80 /*
81 * M5 can do several special things when various signals are sent.
82 * None are mandatory.
83 */
84 void
85 initSignals()
86 {
87 // Floating point exceptions may happen on misspeculated paths, so
88 // ignore them
89 signal(SIGFPE, SIG_IGN);
90
91 // We use SIGTRAP sometimes for debugging
92 signal(SIGTRAP, SIG_IGN);
93
94 // Dump intermediate stats
95 signal(SIGUSR1, dumpStatsHandler);
96
97 // Dump intermediate stats and reset them
98 signal(SIGUSR2, dumprstStatsHandler);
99
100 // Exit cleanly on Interrupt (Ctrl-C)
101 signal(SIGINT, exitNowHandler);
102
103 // Print out cycle number on abort
104 signal(SIGABRT, abortHandler);
105 }
106
107 /*
108 * Uncompress and unmarshal the code object stored in the
109 * EmbeddedPyModule
110 */
111 PyObject *
112 getCode(const EmbeddedPyModule *pymod)
113 {
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,
118 pymod->zlen);
119 if (ret != Z_OK)
120 panic("Could not uncompress code: %s\n", zError(ret));
121 assert(unzlen == pymod->mlen);
122
123 return PyMarshal_ReadObjectFromString((char *)marshalled, pymod->mlen);
124 }
125
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))
129
130 /*
131 * Load and initialize all of the python parts of M5, including Swig
132 * and the embedded module importer.
133 */
134 int
135 initM5Python()
136 {
137 extern void initSwig();
138
139 // initialize SWIG modules. initSwig() is autogenerated and calls
140 // all of the individual swig initialization functions.
141 initSwig();
142
143 // Load the importer module
144 PyObject *code = getCode(&embeddedPyImporter);
145 PyObject *module = PyImport_ExecCodeModule(PyCC("importer"), code);
146 if (!module) {
147 PyErr_Print();
148 return 1;
149 }
150
151 // Load the rest of the embedded python files into the embedded
152 // python importer
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);
158 if (!result) {
159 PyErr_Print();
160 return 1;
161 }
162 Py_DECREF(result);
163 ++pymod;
164 }
165
166 return 0;
167 }
168
169 /*
170 * Start up the M5 simulator. This mostly vectors into the python
171 * main function.
172 */
173 int
174 m5Main(int argc, char **argv)
175 {
176 PySys_SetArgv(argc, argv);
177
178 // We have to set things up in the special __main__ module
179 PyObject *module = PyImport_AddModule(PyCC("__main__"));
180 if (module == NULL)
181 panic("Could not import __main__");
182 PyObject *dict = PyModule_GetDict(module);
183
184 // import the main m5 module
185 PyObject *result;
186 result = PyRun_String("import m5", Py_file_input, dict, dict);
187 if (!result) {
188 PyErr_Print();
189 return 1;
190 }
191 Py_DECREF(result);
192
193 // Start m5
194 result = PyRun_String("m5.main()", Py_file_input, dict, dict);
195 if (!result) {
196 PyErr_Print();
197 return 1;
198 }
199 Py_DECREF(result);
200
201 return 0;
202 }