73d4dbd703e76619abdfc3f0bef1f4cde5e5f4b2
[gem5.git] / src / sim / init.cc
1 /*
2 * Copyright (c) 2012, 2017 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
42 #include <Python.h>
43
44 #include "sim/init.hh"
45
46 #include <marshal.h>
47 #include <zlib.h>
48
49 #include <iostream>
50 #include <list>
51 #include <string>
52 #include <vector>
53
54 #include "base/compiler.hh"
55 #include "base/cprintf.hh"
56 #include "base/logging.hh"
57 #include "base/types.hh"
58 #include "config/have_protobuf.hh"
59 #include "python/pybind11/pybind.hh"
60 #include "sim/async.hh"
61 #include "sim/core.hh"
62
63 #if HAVE_PROTOBUF
64 #include <google/protobuf/stubs/common.h>
65
66 #endif
67
68 using namespace std;
69 namespace py = pybind11;
70
71 // The python library is totally messed up with respect to constness,
72 // so make a simple macro to make life a little easier
73 #define PyCC(x) (const_cast<char *>(x))
74
75 EmbeddedPython *EmbeddedPython::importer = NULL;
76 PyObject *EmbeddedPython::importerModule = NULL;
77 EmbeddedPython::EmbeddedPython(const char *filename, const char *abspath,
78 const char *modpath, const unsigned char *code, int zlen, int len)
79 : filename(filename), abspath(abspath), modpath(modpath), code(code),
80 zlen(zlen), len(len)
81 {
82 // if we've added the importer keep track of it because we need it
83 // to bootstrap.
84 if (string(modpath) == string("importer"))
85 importer = this;
86 else
87 getList().push_back(this);
88 }
89
90 list<EmbeddedPython *> &
91 EmbeddedPython::getList()
92 {
93 static list<EmbeddedPython *> the_list;
94 return the_list;
95 }
96
97 /*
98 * Uncompress and unmarshal the code object stored in the
99 * EmbeddedPython
100 */
101 PyObject *
102 EmbeddedPython::getCode() const
103 {
104 Bytef marshalled[len];
105 uLongf unzlen = len;
106 int ret = uncompress(marshalled, &unzlen, (const Bytef *)code, zlen);
107 if (ret != Z_OK)
108 panic("Could not uncompress code: %s\n", zError(ret));
109 assert(unzlen == (uLongf)len);
110
111 return PyMarshal_ReadObjectFromString((char *)marshalled, len);
112 }
113
114 bool
115 EmbeddedPython::addModule() const
116 {
117 PyObject *code = getCode();
118 PyObject *result = PyObject_CallMethod(importerModule, PyCC("add_module"),
119 PyCC("sssO"), filename, abspath, modpath, code);
120 if (!result) {
121 PyErr_Print();
122 return false;
123 }
124
125 Py_DECREF(result);
126 return true;
127 }
128
129 /*
130 * Load and initialize all of the python parts of M5.
131 */
132 int
133 EmbeddedPython::initAll()
134 {
135 // Load the importer module
136 PyObject *code = importer->getCode();
137 importerModule = PyImport_ExecCodeModule(PyCC("importer"), code);
138 if (!importerModule) {
139 PyErr_Print();
140 return 1;
141 }
142
143 // Load the rest of the embedded python files into the embedded
144 // python importer
145 list<EmbeddedPython *>::iterator i = getList().begin();
146 list<EmbeddedPython *>::iterator end = getList().end();
147 for (; i != end; ++i)
148 if (!(*i)->addModule())
149 return 1;
150
151 return 0;
152 }
153
154 EmbeddedPyBind::EmbeddedPyBind(const char *_name,
155 void (*init_func)(py::module &),
156 const char *_base)
157 : initFunc(init_func), registered(false), name(_name), base(_base)
158 {
159 getMap()[_name] = this;
160 }
161
162 EmbeddedPyBind::EmbeddedPyBind(const char *_name,
163 void (*init_func)(py::module &))
164 : initFunc(init_func), registered(false), name(_name), base("")
165 {
166 getMap()[_name] = this;
167 }
168
169 void
170 EmbeddedPyBind::init(py::module &m)
171 {
172 if (!registered) {
173 initFunc(m);
174 registered = true;
175 } else {
176 cprintf("Warning: %s already registered.\n", name);
177 }
178 }
179
180 bool
181 EmbeddedPyBind::depsReady() const
182 {
183 return base.empty() || getMap()[base]->registered;
184 }
185
186 std::map<std::string, EmbeddedPyBind *> &
187 EmbeddedPyBind::getMap()
188 {
189 static std::map<std::string, EmbeddedPyBind *> objs;
190 return objs;
191 }
192
193 #if PY_MAJOR_VERSION >= 3
194 PyObject *
195 #else
196 void
197 #endif
198 EmbeddedPyBind::initAll()
199 {
200 std::list<EmbeddedPyBind *> pending;
201
202 py::module m_m5 = py::module("_m5");
203 m_m5.attr("__package__") = py::cast("_m5");
204
205 pybind_init_core(m_m5);
206 pybind_init_debug(m_m5);
207
208 pybind_init_event(m_m5);
209 pybind_init_stats(m_m5);
210
211 for (auto &kv : getMap()) {
212 auto &obj = kv.second;
213 if (obj->base.empty()) {
214 obj->init(m_m5);
215 } else {
216 pending.push_back(obj);
217 }
218 }
219
220 while (!pending.empty()) {
221 for (auto it = pending.begin(); it != pending.end(); ) {
222 EmbeddedPyBind &obj = **it;
223 if (obj.depsReady()) {
224 obj.init(m_m5);
225 it = pending.erase(it);
226 } else {
227 ++it;
228 }
229 }
230 }
231
232 #if PY_MAJOR_VERSION >= 3
233 return m_m5.ptr();
234 #endif
235 }
236
237 void
238 registerNativeModules()
239 {
240 auto result = PyImport_AppendInittab("_m5", EmbeddedPyBind::initAll);
241 if (result == -1)
242 panic("Failed to add _m5 to Python's inittab\n");
243 }
244
245 /*
246 * Make the commands array weak so that they can be overridden (used
247 * by unit tests to specify a different python main function.
248 */
249 M5_WEAK const char *m5MainCommands[] = {
250 "import m5",
251 "m5.main()",
252 0 // sentinel is required
253 };
254
255 /*
256 * Start up the M5 simulator. This mostly vectors into the python
257 * main function.
258 */
259 int
260 m5Main(int argc, char **_argv)
261 {
262 #if HAVE_PROTOBUF
263 // Verify that the version of the protobuf library that we linked
264 // against is compatible with the version of the headers we
265 // compiled against.
266 GOOGLE_PROTOBUF_VERIFY_VERSION;
267 #endif
268
269
270 #if PY_MAJOR_VERSION >= 3
271 typedef std::unique_ptr<wchar_t[], decltype(&PyMem_RawFree)> WArgUPtr;
272 std::vector<WArgUPtr> v_argv;
273 std::vector<wchar_t *> vp_argv;
274 v_argv.reserve(argc);
275 vp_argv.reserve(argc);
276 for (int i = 0; i < argc; i++) {
277 v_argv.emplace_back(Py_DecodeLocale(_argv[i], NULL), &PyMem_RawFree);
278 vp_argv.emplace_back(v_argv.back().get());
279 }
280
281 wchar_t **argv = vp_argv.data();
282 #else
283 char **argv = _argv;
284 #endif
285
286 PySys_SetArgv(argc, argv);
287
288 // We have to set things up in the special __main__ module
289 PyObject *module = PyImport_AddModule(PyCC("__main__"));
290 if (module == NULL)
291 panic("Could not import __main__");
292 PyObject *dict = PyModule_GetDict(module);
293
294 // import the main m5 module
295 PyObject *result;
296 const char **command = m5MainCommands;
297
298 // evaluate each command in the m5MainCommands array (basically a
299 // bunch of python statements.
300 while (*command) {
301 result = PyRun_String(*command, Py_file_input, dict, dict);
302 if (!result) {
303 PyErr_Print();
304 return 1;
305 }
306 Py_DECREF(result);
307
308 command++;
309 }
310
311 #if HAVE_PROTOBUF
312 google::protobuf::ShutdownProtobufLibrary();
313 #endif
314
315 return 0;
316 }